     <template >
  <div class="article-container">
    <el-card class="box-card">
      <!-- 头部区域 -->
      <div slot="header" class="clearfix">
        <my-breadcrumb>素材管理</my-breadcrumb>
      </div>
      <div style="padding-bottom: 20px">
        <el-radio-group v-model="isCollected" size="mini" @click='hToggle'>
          <el-radio-button :label="false">全部</el-radio-button>
          <el-radio-button :label="true">收藏</el-radio-button>
        </el-radio-group>
        <el-button
         @click="dialogVisible=true"
         type="success" size="mini" style="float:right">上传图片素材</el-button>
      </div>
       <el-row :gutter="10">
        <el-col  class="img_item" :span="4"  v-for ='image in images' :key='image.id'>
          <el-image
            style="height: 180px;width: 180px"
            :src="image.url"
            fit="cover"
          ></el-image>
          <div class="option"  v-show='!isCollected'>
            <span class="el-icon-star-off"
            @click='hSwitchCollect(image)'
            :class="{red:image.is_collected}">
            </span>
            <span class="el-icon-delete" @click='hDelete(image.id)'></span>
          </div>
        </el-col>
      </el-row>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 1em;"
        background
        :page-size="10"
        layout="prev, pager, next"
        @current-change='hChangePage'
        :total="total_count">
      </el-pagination>
    </el-card>
    <el-dialog
      title="素材上传"
      append-to-body
      :visible.sync="dialogVisible">
      <el-upload
  class="avatar-uploader" name='image' :headers='headers'
  action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
  :show-file-list="false"
  :on-success="hUploadSuccess"
  :before-upload="hBeforeUpload">
  <img v-if='imageUrl' class="avatar" @load="hpreviewImageload" :src='imageUrl'>
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
    </el-dialog>
  </div>
</template>
<script>
// 1.导入组件
import MyBreadcrumb from '@/components/breadcrumb.vue'
export default {
  // 2。注册组件
  components: {
    MyBreadcrumb
  },
  name: 'images',
  data () {
    return {
      imageUrl: '',
      headers: {
        Authorization: `Bearer ${localStorage.getItem('tokenStr')}`
      },
      // 是否是收藏的图片
      isCollected: false,
      images: [],
      // 默认总页数为
      total_count: 0,
      // 当前查询默认为第一页
      curPage: 1,
      // 不显示弹出框
      dialogVisible: false
    }
  },
  created () {
    this.loadImages()
  },
  methods: {
    async  aDel (id) {
      try {
        const res = await this.$http({
          method: 'DELETE',
          url: '/mp/v1_0/user/images/' + id
        })
        console.log(res)
        //  删除成功
        this.$message.success('删除成功')
        // 重新发送请求
        this.loadImages()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 删除
    hDelete (id) {
      this.$confirm('你确定要删除么').then(() => {
        // 确定删除
        this.aDel(id)
      }).catch(() => {
        // 取消
      })
    },
    // 切换收藏
    async hSwitchCollect (image) {
      const imageId = image.id
      console.log(image)
      try {
        const res = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/user/images/' + imageId,
          data: {
            collect: !image.is_collected
          }
        })
        console.log(res)
        this.$message.success('操作成功')
        /* image.is_collected = !image.is_collected */
        this.loadImages()
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    },
    // 预览
    hpreviewImageload () {
      setTimeout(() => {
        this.imageUrl = ''
        // 关闭素材上传弹框
        this.dialogVisible = false
      }, 2000)
    },
    // 上传成功
    hUploadSuccess (res, file) {
      this.imageUrl = res.data.url
      this.loadImages()
    },
    hBeforeUpload (file) {
      // 1.设置图片格式
      const isJPG = file.type === 'image/jpeg'
      // 2.设置图片大小
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传的图片只能是JPG格式！！')
      }
      if (!isLt2M) {
        this.$message.error('上传的图片大小不能超过2MB！！')
      }
      return isLt2M && isJPG
    },
    // 当用户点击上传素材 弹出弹框
    // 当用户点击了分页
    hChangePage (page) {
      // 1.更新页码
      this.curPage = page
      // 2.重新发送请求
      this.loadImages()
    },
    // 切换收藏和全部
    hToggle () {
      // console.log(this.isCollected)
      // 1. 更新页码为1
      this.curPage = 1
      // 2. 发送请求
      this.loadImages()
    },
    async loadImages () {
      const res = await this.$http({
        method: 'GET',
        url: '/mp/v1_0/user/images',
        params: {
          // 是否是收藏的图片
          collect: this.isCollected,
          // 当前查询哪一页
          page: this.curPage,
          // 每行固定10条数据
          per_page: 10
        }
      })
      // 更新总条数
      this.total_count = res.data.data.total_count
      this.images = res.data.data.results
    }
  }
}
</script>
    <style lang="less" scope>
.img_item {
  position: relative;
  box-sizing: border-box;
}
.option {
  position: absolute;
  left: 5px;
  width: 180px;
  bottom: 5px;
  height: 30px;
  line-height: 30px;
  background: rgba(0, 0, 0, 0.3);
  text-align: center;
  color: #fff;
  span {
    margin: 0 30px;
    cursor: pointer;
  }
}
.avatar-uploader {
  text-align: center;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.red{
  color:red;
}
</style>
